package com.stms.servlet;

import com.stms.dao.StudentDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/deleteStudent")
public class DeleteStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String idParam = req.getParameter("id");
        // 检查参数是否为空
        if (idParam == null || idParam.isEmpty()) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing student ID.");
            return;
        }
        // 获取表单数据
        int id = Integer.parseInt(idParam);
        // 调用 DAO 层进行删除
        StudentDao studentDao = new StudentDao();
        // 删除成功返回
        int result = studentDao.deleteStudent(id);
        // 结果重定向页面
        if (result > 0) {
            resp.sendRedirect("success.jsp");
        } else {
            resp.sendRedirect("failure.jsp");
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
